(function($){
    
    $.fn.passwordStrength = function(options) {
    	return this.each(function() {
    		var that = this;
    		that.opts = {};
    		that.opts = $.extend({}, $.fn.passwordStrength.defaults, options);
    
    		that.div = $(that.opts.targetDiv);
    		that.defaultClass = that.div.attr('class');
    
    		that.percents = (that.opts.classes.length) ? 100 / that.opts.classes.length : 100;
    		v = $(this).keyup(function() {
    			if( typeof el == "undefined")
    				this.el = $(this);
    			var s = $.passwordStrength.getPasswordStrength(this.value);
    			var p = this.percents;
    			var t = Math.floor(s / p);
    			if(100 <= s)
    				t = this.opts.classes.length - 1;
    			this.div.removeAttr('class').addClass(this.defaultClass).addClass(this.opts.classes[t]);
    		});
    	});    	
    };
    
    $.fn.passwordStrength.defaults = {
    	classes : Array('is10', 'is20', 'is30', 'is40', 'is50', 'is60', 'is70', 'is80', 'is90', 'is100'),
    	targetDiv : '#passwordStrengthDiv',
    	cache : {}
    };
    
    $.passwordStrength = {};
    $.passwordStrength.getRandomPassword = function(size) {
    	var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    	var size = size || 8;
    	var i = 1;
    	var ret = "";
    	while(i <= size) {
    		$max = chars.length - 1;
    		$num = Math.floor(Math.random() * $max);
    		$temp = chars.substr($num, 1);
    		ret += $temp;
    		i++;
    	}
    	return ret;
    };
    
    $.passwordStrength.getPasswordStrength = function(pwd) {
        var pwdLen = pwd.length;
        if(pwdLen > 5) {
            pwdLen = 5;
        }
        
        var F = pwd.replace(/[0-9]/g, "");
        var G = (pwd.length - F.length);
        if(G > 3) {
            G = 3;
        }
        
        var A = pwd.replace(/\W/g, "");
        var C = (pwd.length - A.length);
        if(C > 3) {
            C = 3;
        }
        
        var B = pwd.replace(/[A-Z]/g, "");
        var I = (pwd.length - B.length);
        if(I > 3) {
            I = 3;
        }
        
        var pwdStrengthValue = ((pwdLen * 10) - 20) + (G * 10) + (C * 15) + (I * 10);
        
        if(pwdStrengthValue < 0) {
            pwdStrengthValue = 0;
        }        
        if(pwdStrengthValue > 100) {
            pwdStrengthValue = 100;
        }
        
        return pwdStrengthValue;
    };    
})($);